package com.wingy.test;

import android.util.Xml;

import com.gcssloop.encrypt.symmetric.AESUtil;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.crypto.Cipher;

public class LifeDiary{

    private String title;
    private String date;
    private String body;

    public LifeDiary(String title, String date) {
        this.title = title;
        this.date  = date;
    }

    public LifeDiary(String title, String date, String body) {
        this.title = title;
        this.date  = date;
        this.body = body;
    }

    public String getTitle() {
        return title;
    }

    public String getDate() {
        return date;
    }

    public String getBody() {
        return body;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setBody(String body) {
        this.body = body;
    }

    private List<LifeDiary> lifeDiaryList = new ArrayList<>();



    //新增日记文件
    public void newLifeDairy(String path, String title, String body, String date, String pwd) {
        if("".equals(pwd))
            pwd = "0000";

        //获取现有日记
        List<LifeDiary> newList = new ArrayList<>();
        newList = this.readLifediary(path, pwd);
        this.readLifediary(path, pwd);
        //加入新日记
        LifeDiary temp = new LifeDiary(title,date,body);
        //从头添加
        newList.add(0, temp);
        try {
            File file = new File(path,"lifediary.xml");
            FileOutputStream fos = new FileOutputStream(file);
            // 获得一个序列化工具
            XmlSerializer serializer = Xml.newSerializer();
            serializer.setOutput(fos, "utf-8");
            // 设置文件头
            serializer.startDocument("utf-8", true);
            serializer.startTag(null, "lifediarys");
            for(int i=0;i<newList.size();i++) {

                serializer.startTag(null, "lifediary");
                serializer.attribute(null, "id", String.valueOf(i));
                // 写title
                serializer.startTag(null, "title");
                serializer.text(newList.get(i).getTitle());
                serializer.endTag(null, "title");
                //写内容
                serializer.startTag(null, "body");
                //加密内容
                String aesStr= AESUtil.aes(newList.get(i).getBody(), pwd, Cipher.ENCRYPT_MODE);
                serializer.text(aesStr);
                serializer.endTag(null, "body");
                //写日期
                serializer.startTag(null, "date");
                serializer.text(newList.get(i).getDate());
                serializer.endTag(null, "date");

                serializer.endTag(null, "lifediary");
            }

            serializer.endTag(null, "lifediarys");
            serializer.endDocument();
            fos.close();
            //Toast.makeText(EditActivity.this, "写入成功", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            //Toast.makeText(EditActivity.this, "写入失败", Toast.LENGTH_SHORT).show();
        }
    }

    //读取日记
    public List<LifeDiary> readLifediary(String path, String pwd) {

        if("".equals(pwd))
            pwd = "0000";

        List<LifeDiary> readList = new ArrayList<>();
        try {

            File newpath = new File(path, "lifediary.xml");
            FileInputStream fis = new FileInputStream(newpath);

            // 获得pull解析器对象
            XmlPullParser parser = Xml.newPullParser();
            // 指定解析的文件和编码格式
            parser.setInput(fis, "utf-8");

            int eventType = parser.getEventType(); // 获得事件类型

            String title = null;
            String body = null;
            String date = null;
            String result;

            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagName = parser.getName(); // 获得当前节点的名称

                switch (eventType) {
                    case XmlPullParser.START_TAG: // 当前等于开始节点
                        if ("lifediary".equals(tagName)) {
                        } else if ("title".equals(tagName)) {
                            title = parser.nextText();
                        } else if ("body".equals(tagName)) {
                            result = parser.nextText();
                            body = AESUtil.aes(result, pwd, Cipher.DECRYPT_MODE);
                        } else if ("date".equals(tagName)) {
                            date = parser.nextText();
                        }

                        break;
                    case XmlPullParser.END_TAG:
                        if("lifediary".equals(tagName)) {
                            LifeDiary a = new LifeDiary(title, date, body);
                            readList.add(a);
                        }
                        break;
                    default:
                        break;
                }
                eventType = parser.next(); // 获得下一个事件类型
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return readList;
    }

    //删除日记
    public void delLifeDairy(String path, int id, String pwd) {
        if("".equals(pwd))
            pwd = "0000";

        //获取现有日记
        List<LifeDiary> delList = new ArrayList<>();
        delList = this.readLifediary(path, pwd);
        //移除目标项
        delList.remove(id);

        try {
            File file = new File(path,"lifediary.xml");
            FileOutputStream fos = new FileOutputStream(file);
            // 获得一个序列化工具
            XmlSerializer serializer = Xml.newSerializer();
            serializer.setOutput(fos, "utf-8");
            // 设置文件头
            serializer.startDocument("utf-8", true);
            serializer.startTag(null, "lifediarys");
            for(int i=0;i<delList.size();i++) {

                serializer.startTag(null, "lifediary");
                serializer.attribute(null, "id", String.valueOf(i));
                // 写title
                serializer.startTag(null, "title");
                serializer.text(delList.get(i).getTitle());
                serializer.endTag(null, "title");
                //写内容
                serializer.startTag(null, "body");
                //加密内容
                String aesStr= AESUtil.aes(delList.get(i).getBody(), pwd, Cipher.ENCRYPT_MODE);
                serializer.text(aesStr);
                serializer.endTag(null, "body");
                //写日期
                serializer.startTag(null, "date");
                serializer.text(delList.get(i).getDate());
                serializer.endTag(null, "date");

                serializer.endTag(null, "lifediary");
            }

            serializer.endTag(null, "lifediarys");
            serializer.endDocument();
            fos.close();
            //Toast.makeText(EditActivity.this, "写入成功", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            //Toast.makeText(EditActivity.this, "写入失败", Toast.LENGTH_SHORT).show();
        }
    }

    //更新日记
    public void updateLifediary (String path, int id, String title, String body, String date, String pwd) {

        if("".equals(pwd))
            pwd = "0000";
        //获取现有日记
        List<LifeDiary> updateList = new ArrayList<>();
        updateList = this.readLifediary(path, pwd);
        //更新新日记
        updateList.get(id).setTitle(title);
        updateList.get(id).setDate(date);
        updateList.get(id).setBody(body);

        try {
            File file = new File(path,"lifediary.xml");
            FileOutputStream fos = new FileOutputStream(file);
            // 获得一个序列化工具
            XmlSerializer serializer = Xml.newSerializer();
            serializer.setOutput(fos, "utf-8");
            // 设置文件头
            serializer.startDocument("utf-8", true);
            serializer.startTag(null, "lifediarys");
            for(int i=0;i<updateList.size();i++) {

                serializer.startTag(null, "lifediary");
                serializer.attribute(null, "id", String.valueOf(i));
                // 写title
                serializer.startTag(null, "title");
                serializer.text(updateList.get(i).getTitle());
                serializer.endTag(null, "title");
                //写内容
                serializer.startTag(null, "body");
                //加密内容
                String aesStr= AESUtil.aes(updateList.get(i).getBody(), pwd, Cipher.ENCRYPT_MODE);
                serializer.text(aesStr);
                serializer.endTag(null, "body");
                //写日期
                serializer.startTag(null, "date");
                serializer.text(updateList.get(i).getDate());
                serializer.endTag(null, "date");

                serializer.endTag(null, "lifediary");
            }

            serializer.endTag(null, "lifediarys");
            serializer.endDocument();
            fos.close();
            //Toast.makeText(EditActivity.this, "写入成功", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            //Toast.makeText(EditActivity.this, "写入失败", Toast.LENGTH_SHORT).show();
        }
    }


    //重写日记
    public void rWLifeDairy(String path, String oldpwd, String newpwd) {
        if("".equals(oldpwd))
            oldpwd = "0000";
        if("".equals(newpwd))
            newpwd = "0000";

        //获取现有日记
        List<LifeDiary> delList = new ArrayList<>();
        delList = this.readLifediary(path, oldpwd);

        try {
            File file = new File(path,"lifediary.xml");
            FileOutputStream fos = new FileOutputStream(file);
            // 获得一个序列化工具
            XmlSerializer serializer = Xml.newSerializer();
            serializer.setOutput(fos, "utf-8");
            // 设置文件头
            serializer.startDocument("utf-8", true);
            serializer.startTag(null, "lifediarys");
            for(int i=0;i<delList.size();i++) {

                serializer.startTag(null, "lifediary");
                serializer.attribute(null, "id", String.valueOf(i));
                // 写title
                serializer.startTag(null, "title");
                serializer.text(delList.get(i).getTitle());
                serializer.endTag(null, "title");
                //写内容
                serializer.startTag(null, "body");
                //加密内容
                String aesStr= AESUtil.aes(delList.get(i).getBody(), newpwd, Cipher.ENCRYPT_MODE);
                serializer.text(aesStr);
                serializer.endTag(null, "body");
                //写日期
                serializer.startTag(null, "date");
                serializer.text(delList.get(i).getDate());
                serializer.endTag(null, "date");

                serializer.endTag(null, "lifediary");
            }

            serializer.endTag(null, "lifediarys");
            serializer.endDocument();
            fos.close();
            //Toast.makeText(EditActivity.this, "写入成功", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            //Toast.makeText(EditActivity.this, "写入失败", Toast.LENGTH_SHORT).show();
        }
    }

}
